package com.suke.czx.modules.sys.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.suke.czx.modules.sys.entity.SysRoleMenu;
import com.suke.czx.modules.sys.mapper.SysRoleMenuMapper;
import com.suke.czx.modules.sys.service.SysRoleMenuService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 应用模块名称: 角色资源
 * 代码描述: 角色资源业务层
 * copyright: AUZAR ALL RIGHTS RESERVED
 * company: AUZAR
 *
 * @author: 颜米晨
 * @date: 2021/08/27 16:31
 */
@Service
@AllArgsConstructor
public class SysRoleMenuServiceImpl extends ServiceImpl<SysRoleMenuMapper, SysRoleMenu> implements SysRoleMenuService {

	/**
	 * 全删全加角色资源关系
	 * @param roleId 角色id
	 * @param menuIdList 资源id数据集
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void fullUpdateRoleMenuList(Long roleId, List<Long> menuIdList) {
		baseMapper.delete(Wrappers.<SysRoleMenu>query().lambda().eq(SysRoleMenu::getRoleId, roleId));
		if(CollUtil.isNotEmpty(menuIdList)) {
			menuIdList.forEach(menuId -> {
				SysRoleMenu menu = new SysRoleMenu();
				menu.setRoleId(roleId);
				menu.setMenuId(menuId);
				baseMapper.insert(menu);
			});
		}
	}

}
